package com.share.dao.impl;

import io.codis.jodis.BoundedExponentialBackoffRetryUntilElapsed;
import io.codis.jodis.JedisResourcePool;
import io.codis.jodis.RoundRobinJedisPool;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.jedis.JedisConverters;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import com.share.dao.IRedisDao;
import com.share.entity.BaseRedisObject;
import com.share.entity.ZookeeperCfg;
import com.share.util.JsonUtils;


/**
 * <p>Title: BaseJodisDao </p>
 * <p>Description: jodis API
 */
@Service
public class BaseJodisDao implements IRedisDao {


	private JedisResourcePool jedisResourcePool;

	private CuratorFramework zkClient;


	@Autowired
	@Qualifier("zkConfigCodis")
	private ZookeeperCfg zookeeperCfg;

	@Autowired
	@Qualifier("poolConfig")
	private JedisPoolConfig jedisPoolConfig;

	@Autowired
	@Qualifier("redisScriptBatchUpdateCache")
	private DefaultRedisScript redisScriptBatchUpdateCache;

	/**
	 * BaseJodisDao.java
	 * @return the jedisResourcePool
	 */
	@PostConstruct
	@Override
	public JedisResourcePool getJedisResourcePool() {
		if(jedisResourcePool == null){
			zkClient = CuratorFrameworkFactory.builder().connectString(zookeeperCfg.getZkAddr())
					.sessionTimeoutMs(zookeeperCfg.getZkSessionTimeoutMs())
					.retryPolicy(new BoundedExponentialBackoffRetryUntilElapsed(
							100, 30000, -1L))
							.build();
			zkClient.start();
			jedisResourcePool = RoundRobinJedisPool.create().
					curatorClient(zkClient, true).poolConfig(jedisPoolConfig).zkProxyDir(zookeeperCfg.getZkProxyDir()).build();
		}
		return jedisResourcePool;
	}


	@Override
	public String get(String key) {	
		try(Jedis jedis = getJedisResourcePool().getResource()){
			return jedis.get(key);			
		}
	}


	@Override
	public boolean exists(String key) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean reNameNx(String oldKey, String newKey) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public void delete(String key) {
		try(Jedis jedis = getJedisResourcePool().getResource()){
			jedis.del(key);
		}
	}

	/* 
	 * description：执行lua脚本
	 */
	@Override
	public void excuteLua(List<String> keys, List<String> values) {
		try(Jedis jedis = getJedisResourcePool().getResource()){
			System.out.println("111111111:"+ redisScriptBatchUpdateCache.getScriptAsString());
			//((RedisOperations<String, Object>) jedis).execute(redisScriptBatchUpdateCache, keys, values.toArray());
			jedis.eval(redisScriptBatchUpdateCache.getScriptAsString(), keys, values);
		}
	}


	@Override
	public void deleteAll() {
		// TODO Auto-generated method stub

	}


	@Override
	public void delete(Collection<String> keys) {
		// TODO Auto-generated method stub

	}


	@Override
	public List<RedisClientInfo> getClientList() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String getRedisPerformance(String section, String key) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<String> getCodisNodes() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String getRedisPerformance(String host, int port, String section,
			String key) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<String> getClientList(String host, int port) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public RedisTemplate<String, Object> getRedisTemplate() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public JedisCluster getJedisCluster() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Boolean expire(String key, long timeout, TimeUnit unit) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Boolean expireAt(String key, Date date) {
		try(Jedis jedis = getJedisResourcePool().getResource()){
			return JedisConverters.toBoolean(jedis.expireAt(key, date.getTime() / 1000));
		}
	}


	@Override
	public long ttl(String key){
		try(Jedis jedis = getJedisResourcePool().getResource()){
			return jedis.ttl(key);
		}
	}


	@Override
	public Set<String> keys(String keyPattern) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Map<String, List<String>> scan(ScanOptions sco) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public <T> boolean setNx(String key, T t) {
		try(Jedis jedis = getJedisResourcePool().getResource()){
			return JedisConverters.toBoolean(jedis.setnx(key, JsonUtils.toJson(t)));
		}
	}


	@Override
	public <T> void set(String key, T t) {
		// TODO Auto-generated method stub

	}


	@Override
	public Long incrBy(String key, long delta) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public <T> boolean hSetNx(String key, String field, T value) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public <T> void hSet(String key, String field, T value) {
		// TODO Auto-generated method stub

	}


	@Override
	public <T> void hmSet(String key, Map<String, T> m) {
		// TODO Auto-generated method stub

	}


	@Override
	public Long hDel(String key, String field) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public <T> T hGet(String key, String field, Class<T> type) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public <T> Map<Object, T> hGetAll(String key, Class<T> type) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public <T> List<T> hmGet(String key, Collection<Object> field) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Long lSet(String key, String value) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Long lmSet(String key, Collection<Object> list) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public <T> List<T> lGet(String key, long count) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Long lDel(String key, long count, String value) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Long lLength(String key) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void lTrim(String key, long count) {
		// TODO Auto-generated method stub

	}


	@Override
	public <T> Long sSet(String key, T values) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public <T> Set<T> sGet(String key) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Long sDel(String key, Object values) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Boolean zAdd(String key, String value, double score) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public double zScore(String key, String value) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public Set zRange(String key, long start, long end) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Long zRemoveRange(String key, long begin, long end) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Long zRemoveRangeByScore(String key, double min, double max) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Long zRemove(String key, Object values) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<Object> addListWithDiffKey(List<BaseRedisObject> list) {
		// TODO Auto-generated method stub
		return null;
	}



}
